package vow.gui.renderer;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;

import econtract.simple.EndContractType;
import econtract.simple.FulfillmentType;
import econtract.simple.IfactType;
import econtract.simple.LivelineViolationType;
import econtract.simple.NormType;
import econtract.simple.ObligationType;
import econtract.simple.SituationElementType;
import econtract.simple.StartContractType;
import econtract.simple.ViolationType;
import econtract.simple.NormType.Prescription;
import econtract.simple.NormType.Situation;

public class NormEntryRenderer extends JTextArea implements ListCellRenderer{

	public NormEntryRenderer(){
        setAutoscrolls(true);
        setOpaque(true);
	}
	
	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		if(isSelected){
            setBackground(Color.cyan);
        }
        else
        	if((index % 2)==0)
        		setBackground(Color.LIGHT_GRAY);
        	else
        		setBackground(Color.white);
		
		StringBuffer buffer = new StringBuffer();
		NormType norm = (NormType) value;
		buffer.append(norm.getName()+"\n");
		Prescription p = norm.getPrescription();
		Situation s = norm.getSituation();
		
		for(SituationElementType element : s.getSituationElement()){
				//append antecedenets
			situationElement(element, buffer);
			buffer.append("\n");
		}
		
		buffer.append("=>"+"\n");
		EndContractType ec;
		if((ec = p.getEndContract())!=null){	
			buffer.append("( end contract on "+ec.getWhen()+" )");
		}
		else{
			for(ObligationType o : p.getObligation()){
				//para o consequente
				buffer.append("( "+ o.getAction().getSender()+" is obliged to "+
							o.getAction().getAction()+
							" to "+o.getAction().getReceiver()+" between "+o.getLiveline()+" and "+ o.getDeadline()
							+")");
			}
		}
        setText(buffer.toString());
        return this;
	}
	
	private StringBuffer situationElement(SituationElementType element, StringBuffer builder){
		if(element instanceof FulfillmentType){
			FulfillmentType o = (FulfillmentType) element;
			builder.append("( " + o.getAction().getSender()+ " fulfilled the"+ o.getAction().getAction()+ " on "+" "+o.getWhen()+" to "+o.getAction().getReceiver()+")");
			return builder;
		}
		if(element instanceof IfactType){
			IfactType ifact = (IfactType) element;
			builder.append("( "+ ifact.getAction().getSender()+" "+ifact.getAction().getAction()+" on "+ ifact.getWhen()+" to "+ifact.getAction().getReceiver()+" )");
			return builder;
		}
		if(element instanceof ViolationType){
			ViolationType v = (ViolationType) element;
			builder.append("( "+ v.getAction().getSender()+ " violated the "+
					v.getAction().getAction()+" to "+v.getAction().getReceiver()+" )");
			return builder;
		}
		if(element instanceof StartContractType){
			StartContractType s = (StartContractType)element;
			builder.append("( contract started on "+ s.getWhen()+" )");
			return builder;
		}
		if(element instanceof LivelineViolationType){
			LivelineViolationType l = (LivelineViolationType) element;
			builder.append("( "+ l.getAction().getSender()+ " violated liveline to "+
					l.getAction().getAction()+" to "+l.getAction().getReceiver()+" )");
			return builder;
		}
		else{
			//Situation elemetn not recognized
			builder.append("( element not recognized )");
			return builder;
		}
	}
	
}